// MarkupParser.h : Declaration of the CMarkupParser

#pragma once

#include "resource.h"       // main symbols
//#include "debugconsole.h"




// IParserCallback
[
	object,
	dual, uuid("22FCC3B4-318E-4A7B-8379-610FCA196A65"),
	helpstring("IParserCallback Interface")
]
__interface IParserCallback : IDispatch
{
	[id(1), helpstring("method OnMarkupText")] HRESULT OnMarkupText([in] MarkupTypeEnum MarkupType, [in] BSTR bstrText);
	[id(3), helpstring("method OnParseComplete")] HRESULT OnParseComplete(void);
	[id(4), helpstring("method OnBeginParse")] HRESULT OnBeginParse(void);
	
};


// IMarkupParser
[
	object,
	uuid("BFF94D44-C7C2-4DD1-8D45-DB7E1A59417C"),
	dual,	helpstring("IMarkupParser Interface"),
	pointer_default(unique)
]
__interface IMarkupParser : IDispatch
{

	[id(1), helpstring("method Parse")] HRESULT Parse([in] BSTR bstrText, [in] IParserCallback* pCallback);
};




string MaskText(char const* str)
{
	string text(str, strlen(str)+1);
	// Mask the <%% and %%> texts so that the parser ignores them


	size_t position = text.find("<%%");
	while (position < string::npos)
	{
		text.replace(position, 3, "\253");
		position = text.find("<%%");
	}

	position = text.find("%%>");
	while (position < string::npos)
	{
		text.replace(position, 3, "\254");
		position = text.find("%%>");
	}

	return text;
}
string UnmaskText(char const* str)
{
	std::string value = str;
	
	size_t position = value.find("\253");
	while (position < string::npos)
	{
		value.replace(position, 1, "<%");
		position = value.find("\253");
	}

	position = value.find("\254");
	while (position < string::npos)
	{
		value.replace(position, 1, "%>");
		position = value.find("\254");
	}
	return value;
}
// CMarkupParser

[
	coclass,
	threading("apartment"),
	support_error_info("IMarkupParser"),
	vi_progid("CodeSmithParser.MarkupParser"),
	progid("CodeSmithParser.MarkupParser.1"),
	version(1.0),
	uuid("11F60BFB-2392-4FD0-88A0-E844CE48B84C"),
	helpstring("MarkupParser Class")
]
class ATL_NO_VTABLE CMarkupParser : 
	public IMarkupParser
{
public:
	CMarkupParser()
	{
	}


	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}
	
	void FinalRelease() 
	{
	}

public:

	STDMETHOD(Parse)(BSTR bstrText, IParserCallback* pCallback);
};

